﻿/*******************
 * 编写人DXY
 * 创建日期2017/9/14
 ******************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Partner.MVC.Models;
using System.IO;
using Newtonsoft.Json;

namespace Partner.MVC.Controllers
{
    public class MerchantController : Controller
    {

        #region  办公服务
        GuyEntities db = new GuyEntities();
        /// <summary>
        ///  我的服务 -- 发布服务
        /// </summary>
        /// <returns></returns>
        public ActionResult PublishServer()
        {
            return View();
        }
        /// <summary>
        ///  我的服务 -- 服务列表
        /// </summary>
        /// <returns></returns>
        public ActionResult ServerList()
        {
            //登陆接收过来的角色id
            var user = Session["LoginUser"] as User_Info;
            var list = db.House_Server.Where(n => n.S_CreatePerson == user.Use_Id && n.IsUse != 3).ToList();  //  0未审核  1通过    2 驳回     3 商家删除   4 管理员删除
            ViewBag.list = list;
            return View();
        }

        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddHouseServer(House_Server model)
        {
            //登陆传信息
            var user = Session["LoginUser"] as User_Info;
            //获取图片二进制流
            string base64str = model.S_Img.Split(',')[1];
            //把二进制流转换成字节流
            byte[] data = Convert.FromBase64String(base64str);
            //Encoding.UTF8.get
            //路径
            string filename = DateTime.Now.ToString("yyyyMMddhhmmsss") + "." + model.S_Img;
            string strflie = AppDomain.CurrentDomain.BaseDirectory + "Img\\";
            strflie += filename;
            //写入流
            //using (Stream f = new FileStream(strflie, FileMode.Create))
            //{
            //    f.Write(data, 0, data.Length);
            //    f.Close();
            //}
            //把路径赋值给model
            model.S_CreatePerson = user.Use_Id;
            model.S_CreaeteTime = DateTime.Now;
            model.S_Img = "/img/" + filename;
            model.IsUse = 0;
            var exists = db.House_Server.Where(n => n.S_Type == model.S_Type && n.S_Name == model.S_Name).FirstOrDefault();
            if (exists == null)
            {
                db.House_Server.Add(model);
                return db.SaveChanges();
            }
            else
            {
                return 0;
            }


        }
        /// <summary>
        /// 加载下拉菜单
        /// </summary>
        /// <returns></returns>
        public string SelectList()
        {
            List<Partner.MVC.Model.S_TypeModel> list = new List<Model.S_TypeModel>
            {
                new Partner.MVC.Model.S_TypeModel { S_TypeId = "装修施工", S_Type = "装修施工" },
                new Partner.MVC.Model.S_TypeModel { S_TypeId = "家具", S_Type = "家具" },
                new Partner.MVC.Model.S_TypeModel { S_TypeId = "搬家服务", S_Type = "搬家服务" },
                new Partner.MVC.Model.S_TypeModel { S_TypeId = "网络服务", S_Type = "网络服务" },
            };
            return JsonConvert.SerializeObject(list);
        }
        /// <summary>
        /// 删除服务信息
        /// </summary>
        /// <param name="S_Id"></param>
        /// <returns></returns>
        public int delservice(int S_Id)
        {
            //根据id查询要删除的信息
            var delservice = db.House_Server.Where(n => n.S_Id == S_Id).FirstOrDefault();
            delservice.IsUse = 3;     //  0未审核  1通过    2 驳回     3 商家删除   4 管理员删除
            var del = db.SaveChanges();
            return del;
        }
        /// <summary>
        /// 根据id反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetserverByid(int S_Id)
        {
            var GetList = db.House_Server.Where(n => n.S_Id == S_Id).FirstOrDefault();
            return JsonConvert.SerializeObject(GetList);
        }
        /// <summary>
        /// 实现修改功能
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UptHouseSouse(int S_Id, Partner.MVC.Models.House_Server model)
        {
            var md = db.House_Server.Where(n => n.S_Id == S_Id).FirstOrDefault();
            md.S_Type = model.S_Type;
            md.S_Name = model.S_Name;
            md.S_Intro = model.S_Intro;
            md.S_Profile = model.S_Profile;
            md.S_Telphone = model.S_Telphone;
            md.S_Img = model.S_Img;
            md.IsUse = model.IsUse;
            db.Entry(md).State = System.Data.EntityState.Modified;
            return db.SaveChanges();
        }
        #endregion

        #region 服务
        /// <summary>
        /// 装修施工类别列表
        /// </summary>
        /// <returns></returns>
        public ActionResult DirectionServiceList()
        {
            var Directory = "装修施工";
            var dire = db.House_Server.Where(n => n.S_Type == Directory && n.IsUse == 1).ToList();
            return View(dire);
        }
        /// <summary>
        /// 家具类别列表
        /// </summary>
        /// <returns></returns>
        public ActionResult FurnitureServiceList()
        {
            var Furniture = "家具";
            var furn = db.House_Server.Where(n => n.S_Type == Furniture && n.IsUse == 1).ToList();
            return View(furn);
        }
        /// <summary>
        /// 搬家服务列表
        /// </summary>
        /// <returns></returns>
        public ActionResult MoveHouseServiceList()
        {
            var MoveHouse = "搬家服务";
            var move = db.House_Server.Where(n => n.S_Type == MoveHouse && n.IsUse == 1).ToList();
            return View(move);
        }
        /// <summary>
        /// 网络服务
        /// </summary>
        /// <returns></returns>
        public ActionResult InternetServiceList()
        {
            var Internet = "网络服务";
            var inter = db.House_Server.Where(n => n.S_Type == Internet && n.IsUse == 1).ToList();
            return View(inter);
        }
        #endregion

        #region 订单记录
        /// <summary>
        ///  订单记录
        /// </summary>
        /// <returns></returns>
        public ActionResult OrderRecord()
        {
            //登陆传信息
            var user = Session["LoginUser"] as User_Info;
            var show = from s in db.User_Info
                       join b in db.House_Order
                       on s.Use_ReallyName equals b.O_Company
                       join ser in db.House_Server
                       on b.S_Id equals ser.S_Id
                       join uer in db.User_Info
                       on b.Use_Id equals uer.Use_Id
                       //删除状态 0未支付  1 已支付  2 用户删除  3 商家删除
                       where b.O_Status == 1 && b.O_Company == user.Use_ReallyName && b.O_Status != 3
                       select new
                       {
                           b.O_Id,
                           ser.S_Type,
                           b.O_Company,
                           b.O_Price,
                           b.O_Num,
                           uer.Use_ReallyName,
                           uer.Use_Telphone
                       };
            var list = from s in show.ToList()
                       select Tuple.Create(
                             s.O_Id,
                             s.S_Type,
                             s.O_Company,
                             s.O_Price,
                             s.O_Num,
                             s.Use_ReallyName,
                             s.Use_Telphone);
            return View(list);
        }
        /// <summary>
        /// 个人中心-----个人信息
        /// </summary>
        /// <returns></returns>
        public ActionResult MyInfo()
        {
            var user = Session["LoginUser"] as User_Info;
            return View(db.User_Info.Where(a => a.Use_Id == user.Use_Id && a.IsUse == 1).FirstOrDefault());
        }
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="O_Id"></param>
        /// <returns></returns>
        public int DelOrder(House_Order model)
        {
            //删除状态 0未支付  1 已支付  2 用户删除  3 商家删除
            var del = db.House_Order.Where(n => n.O_Id == model.O_Id).FirstOrDefault();
            del.O_Status = 3;
            db.Entry(del).State = System.Data.EntityState.Modified;
            return db.SaveChanges();
        }

        #endregion

    }
}
